/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.ArrayList;
import java.util.List;
import model.Employee;
import model.Processowner;
import model.Seller;
import org.hibernate.Query;
import org.hibernate.Session;
import util.ProjetHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class ConnexionCore {

    private static Session session;
    ArrayList<Employee> lstEmp;
    ArrayList<Processowner> lstProcessOwner;
    ArrayList<Seller> lstSeller;

    public ConnexionCore() {
        this.lstEmp = new ArrayList<>();
        this.lstProcessOwner = new ArrayList<>();
        this.lstSeller = new ArrayList<>();
        this.session = ProjetHibernateUtil.getSessionFactory().openSession();
        Query queryEmp = session.getNamedQuery("HQL_ALL_EMPLOYE");
        Query queryPO = session.getNamedQuery("HQL_ALL_PROCESSOWNER");
        Query querySel = session.getNamedQuery("HQL_ALL_SELLER");
        lstEmp.addAll(queryEmp.list());
        lstProcessOwner.addAll(queryPO.list());
        lstSeller.addAll(querySel.list());
     
        System.out.println(lstSeller);
    }

    public List<Employee> getLstEmp() {
        return lstEmp;
    }

    public List<Processowner> getLstProcessOwner() {
        return lstProcessOwner;
    }

    public List<Seller> getLstSeller() {
        return lstSeller;
    }

    public boolean verifEmp(String mail, String password) {
        Boolean trouve = false;
        int i = 0;
        while (i < lstEmp.size() && trouve == false) {
            if (lstEmp.get(i).getEmail().equals(mail) && lstEmp.get(i).getEpassword().equals(password)) {
                trouve = true;
            }
            i++;
        }
        return trouve;
    }

    public boolean verifPO(String mail, String password) {
        Boolean trouve = false;
        int i = 0;
        while (i < lstProcessOwner.size() && trouve == false) {
            if (lstProcessOwner.get(i).getPomail().equals(mail) && lstProcessOwner.get(i).getPopassword().equals(password)) {
                trouve = true;
            }
            i++;
        }
        return trouve;
    }

    public boolean verifSel(String mail, String password) {
        Boolean trouve = false;
        int i = 0;
        while (i < lstSeller.size() && trouve == false) {
            if (lstSeller.get(i).getSmail().equals(mail) && lstSeller.get(i).getSpassword().equals(password)) {
                trouve = true;
            }
            i++;
        }
        System.out.println("vendeur mail : "+mail);
        return trouve;
    }

    public boolean checkCnx(String mail, String password) {
        Boolean trouve = false;

        if (verifEmp(mail, password) == true || verifPO(mail, password) == true || verifSel(mail, password) == true) {
            trouve = true;
        }
        return trouve;

    }

    public boolean verifValidationEmp(String mail, String password) {
        Boolean trouve = false;
        int i = 0;
        while (i < lstEmp.size() && trouve == false) {
            if (lstEmp.get(i).getEcanvalidate() == true && lstEmp.get(i).getEmail().equals(mail) && lstEmp.get(i).getEpassword().equals(password)) {
                trouve = true;
            }
            i++;
        }
        return trouve;
    }

    /**
     * ne marche pas...
     * @param email
     * @return 
     */
    public boolean isDirector(String email) {
        List<Employee> lstDirector = new ArrayList<Employee>();
        boolean res;
        Employee director = (Employee) this.session.getNamedQuery("HQL_EMPLOYEEBYMAIL").setString("mail", email).uniqueResult();

        if (director == null) {
            res = false;
        } else {
            int numEmp = director.getEid();
            lstDirector = this.session.getNamedQuery("SQL_DIRECTOR").list();
            res = lstDirector.contains(numEmp);
//            System.out.println(lstDirector);
//            System.out.println(director);
        }
        return res;
    }

    public boolean isBackoffice(String email) {
        Seller seller = new Seller();
        boolean result;
        seller = (Seller) this.session.getNamedQuery("HQL_ALL_SELLER_BY_MAIL").setString("mail", email).uniqueResult();
        Boolean res = seller.getSbackoffice();
        if (res == null || res == false) {
            result = false;
        } else {
            result = true;
        }
        System.out.println(result);
//        if(){
        return result;

    }

    public static void main(String[] args) throws Exception {

        ConnexionCore co = new ConnexionCore();

        System.out.println(co.verifSel("cecile@gmail.com","isis"));

    }
}
